//
//  RZApiManager.m
//  RZIM
//
//  Created by reyzhang on 2021/7/26.
//

#import "RZApiManager.h"
#import "RZIMConfig.h"

//登录
NSString *const UserLogin = @"chat/login";
//创建单聊,私聊
NSString *const CreatePrivateChat = @"chat/group/single";
//发送消息
NSString *const SendMessage = @"chat/message/send";
//创建群组
NSString *const CreateGroup = @"chat/group/create";
//群组列表，会话列表
NSString *const GroupList = @"chat/group/list";
//群组详情
NSString *const GroupDetail = @"chat/group/detail";
//解散群组
NSString *const DismissGroup = @"chat/group/dismiss";
//邀请进群
NSString *const InviteToJoinGroup = @"chat/group/invite";
//删除会话
NSString *const RemoveConversation = @"chat/group/del";
//群组消息免打扰
NSString *const DisturbGroup = @"chat/group/disturb";
//请离群组中的用户
NSString *const KickUsersOfGroup = @"chat/group/kick";
//离开群组
NSString*const LeaveGroup = @"chat/group/leave";
//修改群组名称
NSString *const UpdateGroupName = @"chat/group/name";
//设置群组置顶
NSString *const SetupGroupToTop = @"chat/group/top";
//修改用户资料
NSString *const UpdateUserInfo = @"chat/login/edit";


/*****************************************************/
//消息列表
NSString *const MessageList = @"chat/message/list";
//文件上传 (语音文件，图片文件，视频文件)
NSString *const FileUpload = @"file/upload";






@implementation RZApiManager

/**
 拼接真实的请求地址
 */
+ (NSString *)realURL:(NSString *)url {
    NSString *baseURL = [RZIMConfig sharedConfig].baseURL;
    if (!baseURL.length || baseURL.length == 0) {
        NSLog(@"未配置接口服务器地址");
        NSAssert(baseURL.length > 0, @"未配置接口服务器地址,应用启动后请在RZIMConfig类中设置");
    }
    NSString *lastChar = [baseURL substringFromIndex:baseURL.length -1];
    NSString *requestUrl;
    if ([lastChar isEqualToString:@"/"]) {
        requestUrl = [baseURL stringByAppendingString:url];
    }else {
        requestUrl = [baseURL stringByAppendingFormat:@"/%@",url];
    }
    NSLog(@"real request : %@",requestUrl);
    return requestUrl;
}



/**
 登录
 */
+ (void)userLoginWithUserId:(NSString *)userId
                       name:(NSString *)name
                   portrait:(NSString *)portrait
                    success:(RZSuccessBlock)RZSuccessBlock
                    failure:(RZFailureBlock)RZFailureBlock {
    NSMutableDictionary *params = @{}.mutableCopy;
    params[@"uid"] = userId;
    params[@"username"] = name;
    params[@"userAvatar"] = portrait;
    
    [NetworkTools postjson:[self realURL:UserLogin] parameters:params succes:^(NSDictionary * _Nonnull dic) {
        !RZSuccessBlock ?: RZSuccessBlock(dic);
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"error:%@",error);
        !RZFailureBlock ?: RZFailureBlock(error);
    }];
}

#pragma mark - 群组相关

/**
 与目标用户建立单聊
 @label 标签，分组会话
 */
+ (void)createPrivateChatWithTargetId:(NSString *)tid
                                label:(NSInteger)label
                              success:(RZSuccessBlock)RZSuccessBlock
                              failure:(RZFailureBlock)RZFailureBlock {
    NSMutableDictionary *params = @{}.mutableCopy;
    params[@"tid"] = tid;
    params[@"label"] = @(label);
    [NetworkTools postjson:[self realURL:CreatePrivateChat] parameters:params succes:^(NSDictionary * _Nonnull dic) {
        !RZSuccessBlock ?: RZSuccessBlock(dic);
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"error:%@",error);
        !RZFailureBlock ?: RZFailureBlock(error);
    }];
}



/**
 发送消息
 */
+ (void)sendMessageWithGroupId:(NSString *)groupId
                       message:(NSString *)message
                   messageType:(RZMessageType)messageType
                     extraData:(id)extraData
                      success:(RZSuccessBlock)RZSuccessBlock
                      failure:(RZFailureBlock)RZFailureBlock {
    NSMutableDictionary *params = @{}.mutableCopy;
    params[@"gid"] = groupId;
    params[@"message"] = message;
    params[@"msgType"] = @(messageType);
    params[@"extraData"] = extraData;
    [NetworkTools postjson:[self realURL:SendMessage] parameters:params succes:^(NSDictionary * _Nonnull dic) {
        !RZSuccessBlock ?: RZSuccessBlock(dic);
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"error:%@",error);
        !RZFailureBlock ?: RZFailureBlock(error);
    }];
}

/**
 创建群组。
 @label标签，可以用来区分
 @uids 加入群组的用户id字符，多用户用,号分割 如： 1,2,3
 */
+ (void)createGroupWithName:(NSString *)name
                   portrait:(NSString *)portrait
                       uids:(NSString *)uids
                      label:(NSInteger)label
                    success:(RZSuccessBlock)RZSuccessBlock
                    failure:(RZFailureBlock)RZFailureBlock {
    NSMutableDictionary *params = @{}.mutableCopy;
    params[@"groupName"] = name;
    params[@"groupAvatar"] = portrait;
    params[@"groupType"] = @(1); //固定上是群聊
    params[@"uidList"] = uids; //加入群组的用户id字符，多用户用,号分割
    params[@"label"] = @(label);
    
    [NetworkTools postjson:[self realURL:CreateGroup] parameters:params succes:^(NSDictionary * _Nonnull dic) {
        !RZSuccessBlock ?: RZSuccessBlock(dic);
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"error:%@",error);
        !RZFailureBlock ?: RZFailureBlock(error);
    }];
}



/**
 群组列表,会话列表
 @types 会话类型，多种会话类型之间用,号分割
 */
+ (void)groupListWithPage:(NSInteger)page
                 pageSize:(NSInteger)pageSize
                groupType:(NSString *)types
                    label:(NSInteger)label
                 queryStr:(NSString *)queryStr
                  success:(RZSuccessBlock)RZSuccessBlock
                  failure:(RZFailureBlock)RZFailureBlock {
    NSMutableDictionary *params = @{}.mutableCopy;
    params[@"pageIndex"] = @(page);
    params[@"pageSize"] =  @(pageSize);
    params[@"label"] = @(label);
    params[@"queryStr"] = queryStr;
    params[@"groupType"] = types;
    
    [NetworkTools postjson:[self realURL:GroupList] parameters:params succes:^(NSDictionary * _Nonnull dic) {
        !RZSuccessBlock ?: RZSuccessBlock(dic);
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"error:%@",error);
        !RZFailureBlock ?: RZFailureBlock(error);
    }];
}

/**
 获取群组详情
 */
+ (void)getGroupDetailWithGroupId:(NSString *)groupId
                        success:(RZSuccessBlock)RZSuccessBlock
                        failure:(RZFailureBlock)RZFailureBlock {
    NSMutableDictionary *params = @{}.mutableCopy;
    params[@"gid"] = groupId;
    
    [NetworkTools postjson:[self realURL:GroupDetail] parameters:params succes:^(NSDictionary * _Nonnull dic) {
        !RZSuccessBlock ?: RZSuccessBlock(dic);
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"error:%@",error);
        !RZFailureBlock ?: RZFailureBlock(error);
    }];
}


/**
 解散群组
 */
+ (void)dismissGroupWithGroupId:(NSString *)groupId
                        success:(RZSuccessBlock)RZSuccessBlock
                        failure:(RZFailureBlock)RZFailureBlock {
    NSMutableDictionary *params = @{}.mutableCopy;
    params[@"gid"] = groupId;
    
    [NetworkTools postjson:[self realURL:DismissGroup] parameters:params succes:^(NSDictionary * _Nonnull dic) {
        !RZSuccessBlock ?: RZSuccessBlock(dic);
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"error:%@",error);
        !RZFailureBlock ?: RZFailureBlock(error);
    }];
}

/**
 邀请进群
 */
+ (void)inviteToJoinGroupWithGroupId:(NSString *)groupId
                             uidList:(NSArray *)uidList
                             success:(RZSuccessBlock)RZSuccessBlock
                             failure:(RZFailureBlock)RZFailureBlock {
    NSMutableDictionary *params = @{}.mutableCopy;
    params[@"gid"] = groupId;
    for (int i=0; i<uidList.count; i++) {
        NSString *key = [NSString stringWithFormat:@"uidList[%ld]",(long)i];
        params[key] = uidList[i];
    }
    
    [NetworkTools postjson:[self realURL:InviteToJoinGroup] parameters:params succes:^(NSDictionary * _Nonnull dic) {
        !RZSuccessBlock ?: RZSuccessBlock(dic);
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"error:%@",error);
        !RZFailureBlock ?: RZFailureBlock(error);
    }];
}





/**
 删除会话
 */
+ (void)removeConversationWithGroupId:(NSString *)groupId
                              success:(RZSuccessBlock)RZSuccessBlock
                              failure:(RZFailureBlock)RZFailureBlock {
    NSMutableDictionary *params = @{}.mutableCopy;
    params[@"gid"] = groupId;
    [NetworkTools postjson:[self realURL:RemoveConversation] parameters:params succes:^(NSDictionary * _Nonnull dic) {
        !RZSuccessBlock ?: RZSuccessBlock(dic);
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"error:%@",error);
        !RZFailureBlock ?: RZFailureBlock(error);
    }];
}


/**
 设置群组的消息免打扰
 */
+ (void)disturbGroupWithGroupId:(NSString *)groupId
                        success:(RZSuccessBlock)RZSuccessBlock
                        failure:(RZFailureBlock)RZFailureBlock {
    NSMutableDictionary *params = @{}.mutableCopy;
    params[@"gid"] = groupId;
    [NetworkTools postjson:[self realURL:DisturbGroup] parameters:params succes:^(NSDictionary * _Nonnull dic) {
        !RZSuccessBlock ?: RZSuccessBlock(dic);
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"error:%@",error);
        !RZFailureBlock ?: RZFailureBlock(error);
    }];
}


/**
 从群组中请离用户, 创建群组的群主有权限请离用户
 @groupId 群组ID
 @uids 请离用户，多用户之间用,号分割
 */
+ (void)kickUsersWithGroupId:(NSString *)groupId
                     uidList:(NSArray *)uidList
                     success:(RZSuccessBlock)RZSuccessBlock
                     failure:(RZFailureBlock)RZFailureBlock {
    
    NSMutableDictionary *params = @{}.mutableCopy;
    params[@"gid"] = groupId;
    for (int i=0; i<uidList.count; i++) {
        NSString *key = [NSString stringWithFormat:@"uidList[%ld]",(long)i];
        params[key] = uidList[i];
    }
    [NetworkTools postjson:[self realURL:KickUsersOfGroup] parameters:params succes:^(NSDictionary * _Nonnull dic) {
        !RZSuccessBlock ?: RZSuccessBlock(dic);
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"error:%@",error);
        !RZFailureBlock ?: RZFailureBlock(error);
    }];
    
}

/**
 用户主动离开群组
 */
+ (void)leaveGroupWithGroupId:(NSString *)groupId
                      success:(RZSuccessBlock)RZSuccessBlock
                      failure:(RZFailureBlock)RZFailureBlock {
    
    
    NSMutableDictionary *params = @{}.mutableCopy;
    params[@"gid"] = groupId;
    [NetworkTools postjson:[self realURL:LeaveGroup] parameters:params succes:^(NSDictionary * _Nonnull dic) {
        !RZSuccessBlock ?: RZSuccessBlock(dic);
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"error:%@",error);
        !RZFailureBlock ?: RZFailureBlock(error);
    }];
}


/**
 修改群组名称
 @groupName: 新的群组名称
 */
+ (void)updateGroupNameWithGroupId:(NSString *)groupId
                         groupName:(NSString *)groupName
                           success:(RZSuccessBlock)RZSuccessBlock
                           failure:(RZFailureBlock)RZFailureBlock {
    NSMutableDictionary *params = @{}.mutableCopy;
    params[@"gid"] = groupId;
    params[@"groupName"] = groupName;
    [NetworkTools postjson:[self realURL:UpdateGroupName] parameters:params succes:^(NSDictionary * _Nonnull dic) {
        !RZSuccessBlock ?: RZSuccessBlock(dic);
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"error:%@",error);
        !RZFailureBlock ?: RZFailureBlock(error);
    }];
}

/**
 设置群组置顶
 */
+ (void)setupGroupToTopWithGroupId:(NSString *)groupId
                       success:(RZSuccessBlock)RZSuccessBlock
                           failure:(RZFailureBlock)RZFailureBlock {
    NSMutableDictionary *params = @{}.mutableCopy;
    params[@"gid"] = groupId;
    [NetworkTools postjson:[self realURL:SetupGroupToTop] parameters:params succes:^(NSDictionary * _Nonnull dic) {
        !RZSuccessBlock ?: RZSuccessBlock(dic);
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"error:%@",error);
        !RZFailureBlock ?: RZFailureBlock(error);
    }];
}

#pragma mark - 消息相关
/**
 获取群组消息
 */
+ (void)messageListWithGroupId:(NSString *)groupId
                          page:(NSInteger)page
                      pageSize:(NSInteger)pageSize
                      queryStr:(NSString *)queryStr
                       success:(RZSuccessBlock)RZSuccessBlock
                       failure:(RZFailureBlock)RZFailureBlock {
    NSMutableDictionary *params = @{}.mutableCopy;
    params[@"gid"] = groupId;
    params[@"pageIndex"] = @(page);
    params[@"pageSize"] = @(pageSize);
    params[@"queryStr"] = queryStr;
    
    [NetworkTools postjson:[self realURL:MessageList] parameters:params succes:^(NSDictionary * _Nonnull dic) {
        !RZSuccessBlock ?: RZSuccessBlock(dic);
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"error:%@",error);
        !RZFailureBlock ?: RZFailureBlock(error);
    }];
}



/**
 文件上传
 */
+ (void)uploadWithFiles:(NSArray *)files
               progress:(void(^)(int progress))progressBlock
                success:(RZSuccessBlock)RZSuccessBlock
                failure:(RZFailureBlock)RZFailureBlock {
    
    [NetworkTools uploadWithURL:[self realURL:FileUpload] files:files progress:^(NSProgress * _Nonnull progress) {
        int progressV =  (int)(progress.completedUnitCount/progress.totalUnitCount);
        NSLog(@"current progress : %ld ",(long)progressV);
        !progressBlock ?: progressBlock(progressV);
    } success:^(NSDictionary * _Nonnull dic) {
        !RZSuccessBlock ?: RZSuccessBlock(dic);
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"error:%@",error);
        !RZFailureBlock ?: RZFailureBlock(error);
    }];

    
    
}


/**
 修改用户信息
 */
+ (void)updateUserInfoWithUserId:(NSString *)uid
                            name:(NSString *)name
                          avatar:(NSString *)avatar
                         success:(RZSuccessBlock)RZSuccessBlock
                         failure:(RZFailureBlock)RZFailureBlock{
    NSMutableDictionary *params = @{}.mutableCopy;
    params[@"uid"] = uid;
    params[@"username"] = name;
    params[@"userAvatar"] = avatar;
    
    [NetworkTools postjson:[self realURL:UpdateUserInfo] parameters:params succes:^(NSDictionary * _Nonnull dic) {
        !RZSuccessBlock ?: RZSuccessBlock(dic);
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"error:%@",error);
        !RZFailureBlock ?: RZFailureBlock(error);
    }];
}




@end
